﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// 面板基类
/// 功能:
/// 1.用于查找所有控件对象
/// 2.提供显示 隐藏函数
/// </summary>
public class UIBase : MonoBehaviour
{
    private Dictionary<string, List<UIBehaviour>> controlDic = new Dictionary<string, List<UIBehaviour>>();

    private void Awake()
    {
        FindChildControl<Button>();
        FindChildControl<Text>();
        FindChildControl<Image>();
        FindChildControl<Toggle>();
    }

    private void FindChildControl<T>() where T : UIBehaviour
    {
        T[] controls = GetComponentsInChildren<T>();
        for (int i = 0; i < controls.Length; i++)
        {
            string uiName = controls[i].gameObject.name;

            if (controlDic.ContainsKey(uiName))
                controlDic[uiName].Add(controls[i]);
            else
                controlDic.Add(uiName, new List<UIBehaviour>() { controls[i] });
        }
    }

    public T GetControl<T>(string name) where T : UIBehaviour
    {
        if (controlDic.ContainsKey(name))
        {
            foreach (var control in controlDic[name])
            {
                if (control is T)
                    return control as T;
            }
        }
        return null;
    }

    public virtual void Show()
    {

    }

    public virtual void Hide()
    {

    }
}
